// pages/List/List.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
let app=getApp();
Page({
  data: {
    plan:false,
    medicine:false,
    anniversary:false,
    dialogShow:false,
    buttons: [{text: '是'}, {text: '否'}],
    openid:app.globalData.openid,
    jinid:"",//要删除的纪念日id
    anniversaryList:[],
    DrugList:[],
    planlist:[],
    showempty:false,
    start:"开启",
    close:"关闭",
    activeName: '1',
    planlength:1,
    adlength:1,
    medicinelength:1,
  },
  onChangePlan(event)
  {
    this.setData({
      activeName: event.detail,
    });
  },
//获取纪念日信息
getAnniversary(){
  let that=this;
  that.setData({
   anniversaryList:[]
  });
    wx.request({
      url:app.globalData.url+'/findJiNianByuser', 
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
      data: {
        openid:that.data.openid,
       },
       method: 'post',
       success: function (res) {
            if(res.data.length==0)
            {
              that.setData({
                adlength:0
              })
            }
            else
            {
              that.setData({
                anniversaryList:res.data,
                adlength:1
              });
            }
          
       },
       fail:function(err){//请求失败之后要做什么
          console.log('请求失败')
          }
          
  })
},
//药品状态修改
modifytai(e)
{
  let that=this;
  Dialog.confirm({
    title: '药单暂停提示',
    message: '你确定要暂时关闭该药单吗？',
  })
    .then(() => {
     if(e.currentTarget.dataset.tai==1)
     {
      that.pausedrug(e.currentTarget.dataset.id);
      Notify({ type: 'success', message: '药单暂停成功！' });
     }
     else{
      that.reusedrug(e.currentTarget.dataset.id);
      Notify({ type: 'success', message: '药单重启成功！' });
     }
    })
    .catch(() => {
      Notify({ type: 'success', message: '药单取消操作成功！' });
    });
},
//暂停药单状态
pausedrug(e)
{
  let that=this;
  wx.request({
    url:app.globalData.url+'/pausemedicine', 
    header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
    data: {
      drugListRecordid:e
     },
     method: 'post',
     success: function (res) {
          that.getDrugList();  
     },
     fail:function(err){//请求失败之后要做什么
        console.log('请求失败')
        }
})
},
//重启药单
reusedrug(e)
{
  let that=this;
  wx.request({
    url:app.globalData.url+'/reusemedicine', 
    header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
    data: {
      drugListRecordid:e
     },
     method: 'post',
     success: function (res) {
          that.getDrugList();  
     },
     fail:function(err){//请求失败之后要做什么
        console.log('请求失败')
        }
})
},
//添加纪念日信息
skiptoaddJinian(){
  wx.navigateTo({
    url: '/pages/addAnniversary/addAnniversary',
  })
},
//纪念日删除
openConfirm: function (e) {
  this.setData({
      dialogShow: true
  })
  this.data.jinid=e.currentTarget.dataset.id;
},
deletejinian(e)
{
  let that=this;
  this.setData({
    dialogShow:false
  })
  if(e.detail.item.text=="是")
  {
  console.log(this.data.jinid);
  wx.request({
    url:app.globalData.url+'/deletejinian', 
    header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
    data: {
     jinid:this.data.jinid,
     },
     method: 'post',
     success: function (res) {
          that.getAnniversary();
          wx.showToast({
            title: '消除成功！',
            icon:"success",
            duration:1000,
          })
     },
     fail:function(err){//请求失败之后要做什么
        console.log('请求失败',err);
        wx.showToast({
          title: '消除失败！',
          icon:"error",
          duration:1000
        })
        }
})
  }
},
//管理纪念日（暂停还是继续）
onChange(e) {
  let that=this;
  wx.showModal({
    title: '重要提示',
    content: '是否执行操作？',
    success: (res) => {
      if (res.confirm) {
        if(e.currentTarget.dataset.tai==1)
        {
          that.pausejob(e.currentTarget.dataset.id);
        }
        else{
          that.reusejob(e.currentTarget.dataset.id);
        }
       
      }
    },
  });
},
//暂停工作
pausejob(e)
{
  let that=this;
  wx.request({
    url:app.globalData.url+'/pausejinian', 
    header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
    data: {
       jinid:e
     },
     method: 'post',
     success: function (res) {
          console.log("成功");
          wx.showToast({
            title: '正在暂停...',
            icon:"loading"
          });
          that.getAnniversary();  
     },
     fail:function(err){//请求失败之后要做什么
        console.log('请求失败')
        }
})
},
//继续工作
reusejob(e)
{
  let that=this;
  wx.request({
    url:app.globalData.url+'/reusejinian', 
    header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
    data: {
       jinid:e
     },
     method: 'post',
     success: function (res) {
          console.log("成功");
          wx.showToast({
            title: '正在开启...',
            icon:"loading",
          })
          that.getAnniversary();  
     },
     fail:function(err){//请求失败之后要做什么
        console.log('请求失败')
        }
})
},

//跳转到编辑页面
goAnniversary(e){

  wx.navigateTo({
    url: '/pages/addAnniversary/addAnniversary',
  });
},
//跳转到药品添加页面
skiptoaddMedicine()
{
  wx.navigateTo({
    url: '/pages/Medicine/Medicine',
  });

},
  onLoad: function (options) {
    let that=this;
  if(options.id=="1")
  {
    that.setData({
      anniversary:true
    });
    that.getAnniversary();
    

  }
  else if(options.id=="2")
  {
    that.setData({
      medicine:true
    })
    that.getDrugList();

  }
  else if(options.id=="3")
  {
    that.setData({
      plan:true
    })
    that.getPlanInfo();
  }
  },
//获取药单信息
getDrugList()
{
  let that=this;
  that.setData({
    DrugList:[]
  });
  wx.request({
    url:app.globalData.url+'/findListDrug', 
    header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
    data: {
        openid:app.globalData.openid,
     },
     method: 'post',
     success: function (res) {
          if(res.data.length==0)
          {
            that.setData({
              medicinelength:0
            })
          }
          else{
            that.setData({
              DrugList:res.data,
              medicinelength:1
            })  ;
          } 
     },
     fail:function(err){//请求失败之后要做什么
        console.log('请求失败')
        }
})
},
//添加计划
goPlan(){
  wx.navigateTo({
    url: '/pages/Plan/Plan',
  })
},
//获取计划信息
getPlanInfo(){
  let that=this;
  that.setData({
    planlist:[]
  });
  wx.request({
    url:app.globalData.url+'/findAllPlan', 
    header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
    data: {
        openid:app.globalData.openid,
     },
     method: 'post',
     success: function (res) {
          if(res.data.length==0)
         {
           that.setData({
             planlength:0
           })
         }
         else
         {
          that.setData({
            planlist:res.data,
            planlength:1
          });
         }
     },
     fail:function(err){//请求失败之后要做什么
        console.log('请求失败')
        }
})
},
//删除计划信息
deleteplan(e){
  let that=this;
  wx.request({
    url:app.globalData.url+'/deletePlan', 
    header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
    data: {
       planid:e.currentTarget.dataset.id
     },
     method: 'post',
     success: function (res) {
         that.getPlanInfo();
     },
     fail:function(err){//请求失败之后要做什么
        console.log('请求失败')
        }
})
},
onShow: function() {
  this.getAnniversary();
  this.getDrugList();
  this.getPlanInfo();
},
  onShareAppMessage: function () {
  }
})